/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#include "MkHttpUtil.h"
#include "MkHttpResponse.h"
#include "MkUtil/MkUtil.h"
#include "MkUtil/MkLog.h"

CMkHttpResponse::CMkHttpResponse()
    :m_StatusCode(MkHttpStateContinue)
{

}

void CMkHttpResponse::Release()
{
    m_StatusCode = MkHttpStateContinue;
    CMkHttpMessage::Release();
}

Uint32 CMkHttpResponse::OnParseHeaderLine(const MkString& HeaderLine)
{
    /*http://mirrors.nju.edu.cn/rfc/beta/errata/rfc2616.html 6.1
     Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF
     HTTP-Version like HTTP/1.1
    */
    MkVector<MkString> VecString;
    Uint32 ErrorCode = CMkUtil::StringSplit(HeaderLine, " ", VecString);
    MkCheckErrorLog(ErrorCode, "split string:%s failed\n", HeaderLine.c_str());
    if (VecString.size() < 3 || 0 != VecString[0].find("HTTP")) {
        return InvalidParams;
    }
    m_StatusCode = (MkHttpStatusCode)std::stoi(VecString[1]);
    MkVector<MkString> vecTmpString;
    ErrorCode = CMkUtil::StringSplit(VecString[0], "/", vecTmpString);
    MkCheckErrorLog(ErrorCode, "get version:%s failed\n", VecString[2].c_str());
    if (vecTmpString.size() < 2) {
        return InvalidParams;
    }
    SetVersion(vecTmpString[1]);
    return NoneError;
}

Uint32 CMkHttpResponse::GetHeaderLine(MkString& HeaderLine)
{
    HeaderLine.clear();
    HeaderLine += "HTTP/" + GetVersion() + " " + std::to_string(m_StatusCode) + " " + CMkHttpUtil::GetInstance().GetCodeDesc(m_StatusCode) + MkEndLine;
    return NoneError;
}

CMkHttpResponse::~CMkHttpResponse()
{

}